package com.lam.generator.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.ValidOperType;
import com.lam.generator.common.constant.GeneratorConstant;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lam.framework.operLog.annotation.Log;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.framework.annotation.ParamInject;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.generator.entity.GenCodeModule;
import com.lam.generator.param.GenCodeModuleParam;
import com.lam.generator.service.GenCodeModuleService;
import com.lam.common.components.easyexcel.export.DownloadUtil;
import com.lam.generator.entity.imp.GenCodeModuleImp;
import com.lam.generator.listener.UploadGenCodeModuleListener;
import java.util.Collections;
import java.util.Map;
import com.lam.common.components.easyexcel.imp.ImportUtil;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.write.handler.WriteHandler;
import org.springframework.validation.annotation.Validated;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.group.Add;

/**
 * 代码模块Controller
 * 
 * @author admin
 * @date 2022-11-01
 */
@Log(title = "代码模块")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/tool/codeModule")
public class GenCodeModuleController extends BaseController {

    @Autowired
    private GenCodeModuleService genCodeModuleService;

    /**
     * 查询代码模块列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('tool:codeModule:list')")
    @GetMapping("/list")
    public Result list(GenCodeModuleParam param) {
    
        startPage();
        List<GenCodeModule> list = genCodeModuleService.selectList(param);
        return Result.ok(getTableData(list));
    }

    /**
     * 导出代码模块列表
     */
    @PreAuthorize("@ss.hasPm('tool:codeModule:export')")
    @Log(businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(GenCodeModuleParam param) {
    
        withOrderBy();
        List<GenCodeModule> list = genCodeModuleService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("代码模块导出", list, GenCodeModule.class));
    }

    @Log(businessType = BusinessType.EXPORT, oper = "下载导入模板")
    @PreAuthorize("@ss.hasPm('tool:codeModule:import')")
    @GetMapping("/downloadTemplate")
    public Result downloadTemplate() {
        WriteHandler headRowCommentWriteHandler = ImportUtil.getHeadRowCommentWriteHandler(GenCodeModuleImp.class, GenCodeModule.class, Add.class);
        WriteHandler dropDownWriteHandler = ImportUtil.getDropDownWriteHandler(GenCodeModuleImp.class, Add.class);
        return Result.ok(DownloadUtil.exportExcel("代码模块导入模板", Collections.emptyList(), GenCodeModuleImp.class, 
            headRowCommentWriteHandler, dropDownWriteHandler));
    }
    
    @Log(businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPm('tool:codeModule:import')")
    @PostMapping("/importData")
    public Result importData(MultipartFile file, boolean updateSupport) {
        
        logger.info("代码模块数据导入, 开始接收文件数据...");
        UploadGenCodeModuleListener readListener = new UploadGenCodeModuleListener(genCodeModuleService);
        readListener.setUpdateSupport(updateSupport);
        ImportUtil.importData("代码模块", file, readListener);
        Map<String, Object> data = readListener.getOperResult();
        data.put("originalFilename", file.getOriginalFilename());
        return Result.ok(data);
    }

    /**
     * 获取代码模块详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('tool:codeModule:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(genCodeModuleService.selectById(id));
    }

    /**
     * 新增代码模块
     */
    @PreAuthorize("@ss.hasPm('tool:codeModule:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody GenCodeModule genCodeModule) {
    
        ValidateUtil.validUnique(genCodeModule, ValidOperType.ADD, GeneratorConstant.UNIQUE_CODE_MODULE, "模块已存在！");
        return toAjax(genCodeModuleService.insert(genCodeModule));
    }

    /**
     * 修改代码模块
     */
    @PreAuthorize("@ss.hasPm('tool:codeModule:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody GenCodeModule genCodeModule) {
    
        ValidateUtil.validUnique(genCodeModule, ValidOperType.UPDATE, GeneratorConstant.UNIQUE_CODE_MODULE, "模块已存在！");
        return toAjax(genCodeModuleService.update(genCodeModule));
    }

    /**
     * 删除代码模块
     */
    @PreAuthorize("@ss.hasPm('tool:codeModule:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(genCodeModuleService.deleteById(id));
    }

}
